package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Game {

	private ListWords listWords;
	private Player aPlayer;
	private ArrayList<String> gamesyllables;
	private HashSet<Word> wordFound=new HashSet<Word>();
	private int nbWordFound;
	
	/*public Game(ListWords lw, Player aPlayer) {
		setListWords(lw);
		this.setaPlayer(aPlayer);
		gamesyllables=new ArrayList<String>();
	}*/


	public Game(String fileName, int id, int nbSyllable) {
		createPlayer(id);
		createListWords(fileName);
		gamesyllables=listWords.getListSyllable(nbSyllable);
		nbWordFound=0;
		
		
	}
	
	/**
	 * 
	 * @param syllable
	 * @return
	 */
	public List<Word> getListContainPrefix(String prefix, int index) {
		ArrayList<Word> temp=new ArrayList<Word>();
		
		for(Word word:listWords.getListWord()) {
			if(word.getWord().startsWith(prefix, index))
				temp.add(word);
		}
		return temp;
	}
	
	
	/**
	 * 
	 */
	/*public Game() {
		listWords=new ListWords();
		aPlayer=new Player(1);
		gamesyllables=new ArrayList<String>();
	}*/
	public Game() {
		listWords=new ListWords();
		aPlayer=new Player(1);
		gamesyllables=new ArrayList<String>();
	}

	/**
	 * 
	 * @param fileName
	 */
	private void createListWords(String fileName) {
		listWords=new ListWords(fileName);
	}
	
	private void createPlayer(int id) {
		aPlayer=new Player(id);
	}
	
	public ArrayList<String> getGameSyllables() {
		return gamesyllables;
	}
	
	/**
	 * 
	 * @param fileName
	 * @param id
	 * @param nbSyllable
	 */
	public void play() {
		
		List<Word> temp1;
		List<Word> temp2=new ArrayList<Word>();
		
		
		Scanner sc=new Scanner(System.in); 
		
		while(gamesyllables.size() >0 ) {
			System.out.println(gamesyllables);
			System.out.print(">");
			String s=sc.next();
			
			temp1=getListContainPrefix(s,0);
			if(temp1!=null) {
				String suf=sc.next();
				temp2=getListContainPrefix(suf,s.length());
				if(temp2!=null) {
					System.out.println(temp2);
					wordFound.add(temp2.get(0));
					temp1.clear();
					temp2.clear();
					nbWordFound++;
					gamesyllables.remove(s);
					gamesyllables.remove(suf);
				}
			}
		
			//System.out.println(gamesyllables);
		}
		System.out.println(gamesyllables);
		
		
	}
	
	
	/**
	 * @param aPlayer the aPlayer to set
	 */
	public void setaPlayer(Player aPlayer) {
		this.aPlayer = aPlayer;
	}

	/**
	 * @return the aPlayer
	 */
	public Player getaPlayer() {
		return aPlayer;
	}


	/**
	 * @param listWords the listWords to set
	 */
	public void setListWords(ListWords listWords) {
		this.listWords = listWords;
	}


	/**
	 * @return the listWords
	 */
	public ListWords getListWords() {
		return listWords;
	}

	public static void main(String args[]) {
		Game game=new Game("/home/hady/Bureau/Devint2012/src/model/vehicule.txt",1,6);
		
		
		game.play();
		
	}
}
